<?php

namespace app\common\model;

use app\common\model\base\BaseModel;
use app\common\traits\InstanceTrait;

class Article extends BaseModel
{
    use InstanceTrait;

    public function __construct($data = [], Model $parent = null, $table = '')
    {
        parent::__construct($data, $parent, $table);
        $this->pk = 'article_id';
        $this->initBaseField();
    }

    /**
     * 基础的where,所有查询都应以此为查询条件
     * @param array $param
     * @return mixed
     */
    public function getBaseWhere($param = [], $alias = '')
    {
        $where = parent::getBaseWhere($param, $alias);
        if (!empty($param['keywords'])) {
            $where[] = ['title', 'like', "%{$param['keywords']}%"];
        }
        if (!empty($param['admin_id'])) {
            $where[] = ['creater_id', '=', (string)($param['admin_id'])];
        }
        return $where;
    }

    public function getContentAttr($value)
    {
        return html_entity_decode(stripcslashes($value));
    }

    public function category($value)
    {
        return $this->belongsTo(Cate);
    }

    /**
     * 获取所有针对文章的评论。
     */
    public function comments()
    {
        return $this->morphMany(Comment::class, 'commentable');
    }
}
